from chhLib.Pachong import Pachong
import pandas as pd
def dangdangSingle(page):
    url =  f'https://search.dangdang.com/?key=python&category_path=01.00.00.00.00.00&page_index={page}'
    titleSelector = 'ul > li > p.name > a'

    parser = '<p class="name" name="title"><a title=".*?" ddclick="act=normalResult_title&amp;pos=.*?" name="itemlist-title" dd_name="单品标题" target="_blank"> <font class="skcolor_ljg">Python</font>(.*?)</a></p>'
    dangdang = Pachong(url=url,selector=titleSelector,parserText=parser)
    html = dangdang.get_html4requests()
    titles = dangdang.parse_html(html)
    titles = dangdang.clearContent(titles)
    dangdang.selector = 'ul > li > p.price > span.search_now_price'
    prices =  dangdang.parse_html(html)
    prices=dangdang.clearContent(prices)
    # content = list(zip(titles,prices))
    content = {'书名':titles,'价格':prices}
    content = pd.DataFrame(content)
    return content

if __name__ == '__main__':
    all_content = pd.DataFrame()
    for i in range(1,7):
        content = dangdangSingle(i)
        all_content = pd.concat([all_content,content])
    
    all_content.to_excel('dangdang.xlsx',index=False)
    # print(content)
